<script src="http://yui.yahooapis.com/3.8.1/build/yui/yui-min.js"></script>
<script type="text/javascript">
	var rand = Math.floor(Math.random() * 2) + 1;
	var ws = new WebSocket("ws://" + document.location.host + "/websocket/testWebsocket");
	var orderWs = new WebSocket("ws://" + document.location.host
			+ "/websocket/chat");

	ws.onmessage = function(evt) {
		console.log(evt.data);
		var page = JSON.parse(evt.data);
		YUI().use("datatable", function(Y) {
			var columnDef = [ {
				key : "artikkel",
				label : "Artikkel",
				abbr : "Art.no"
			}, {
				key : "nimetus",
				label : "Nimetus",
				abbr : "Price"
			}, {
				key : "hind",
				label : "Hind + KM",
				abbr : "Price"
			} ];

			// A table from data with keys that work fine as column names
			var simple = new Y.DataTable({
				columns : columnDef,
				data : page,
				summary : "Price sheet for inventory parts",
				caption : "Tellimusel olevad tooted"
			});
			document.getElementById("simple").innerHTML = "";
			simple.render("#simple");
		});
	};

	function sendMessage(arg) {
		ws.send(arg);
	}

	orderWs.onmessage = function(evt) {
		/*
		console.log(evt.data);
		var data = JSON.parse(evt.data);
		document.getElementById("chart").innerHTML = "";
		YUI().use('charts', function (Y) {    		    
			var mychart = new Y.Chart({
		        dataProvider:data, 
		        render:"#chart", 
		        categoryKey:"date", 
		        seriesKeys:["amount"],
		        type:"line"
		    });
		});    */
		var previous = document.getElementById("chat").innerHTML;
		previous = previous + "\n" + evt.data;
		document.getElementById("chat").innerHTML = previous;
	};

	function sendChatMessage() {
		var message = document.getElementById("chatInsert").value;
		if(document.getElementById("isPrivate").checked == true){
			message = "toAdmin#" + message;
		}
		orderWs.send(message);
		document.getElementById("chatInsert").value = "";
	}
	YUI().use('event-key', function(Y) {
		Y.one('#chatInsert').on('key', sendChatMessage, 'enter');
	});
</script>